<?php
require_once("header.php");

$blockFileTypes = array("php","php3","php4","php5","asp","aspx","jsp","json","js","exe","py","pl","jar");

if(isset($_FILES["userfile"])){
	//phpinfo();
	$path = $currentPath ;
	$userfile = $_FILES["userfile"]["tmp_name"];
	$filesize = $_FILES["userfile"]["size"];
	$filename = $_FILES["userfile"]["name"];
	$fileError = $_FILES["userfile"]["error"] ;
	$filename = str_replace(' ', '_', $filename) ;
	 
	$filePath = $path.$filename ;
	
	$string = strtolower($filename);
	if(strpos(".php",$string) != false){
		render_js("parent.set_text('errors','您所上传的文件类型不被允许！');");
	}else if(in_array(get_file_type($filename), $blockFileTypes)){
		render_js("parent.set_text('errors','您所上传的文件类型不被允许！');");
	}else if($fileError != 0){
	    //$labels = label("common.file_size_error");
	    $labels = '未知错误';
	    if($fileError == 1){
	       $labels = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
	    }else if($fileError == 2){
	       $labels = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
	    }else if($fileError == 3){
	       $labels = '文件只有部分被上传';
	    }else if($fileError == 4){
	       $labels = '没有文件被上传';
	    }else if($fileError == 6){
	       $labels = '找不到临时文件夹';
	    }else if($fileError == 7){
	       $labels = '文件写入失败（磁盘空间满）';
		}
	       		
		render_js("parent.set_text('errors','$labels');");
	}else{
		//	echo $path ;
		$ingore = true ;
		$savename = to_os_encoding($filePath);
		if(basename($root) == 'attachment'){
			$check = checkAttachName($filename,$path,true,false) ;
			if($check){
				$labels = label("common.file_exists");
				render_js("alert('$labels'); ");
				return ;
			}else if(file_exists($savename)){
				$savename = to_os_encoding($path.date('ymdHis').$filename);
				$ingore = false ;
			}
			
			$realPath = insetAttachment($filename,$filesize) ;
			 
			if($realPath != 'fail'){
				countAttachSize($filesize) ;
			}
			$savename = to_os_encoding($realPath) ;
			 
		}
		
		if(file_exists($savename) && $ingore){
			$labels = label("common.file_exists");
			render_js("alert('$labels'); ");
			return ;
		}else{
			if(move_uploaded_file($userfile, $savename)){
				
			}
		}
		render_js("parent.do_refresh(); ");
	}
	
	return ;
}else{
	//phpinfo();
}


require_once("./footer.php");